//
//  AXShoppingCartBottomBar.m
//  hwacdx 购物车底部结算工具条
//
//  Created by hwacdx on 2019/5/2.
//  Copyright © 2019 hwacdx. All rights reserved.
//

#import "AXShoppingCartBottomBar.h"

@interface AXShoppingCartBottomBar()

/**
 全选按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *allSelectedBtn;

/**
 合计文本
 */
@property (weak, nonatomic) IBOutlet UILabel *totalLabel;

/**
 价格文本
 */
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;

/**
 结算按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *settlementBtn;

/**
 价格文本的宽度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *priceLabelWidthConstraint;

@end

@implementation AXShoppingCartBottomBar

- (void)setPriceText:(NSString *)priceText{
    _priceText = priceText;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@", priceText];
    
    //计算价格文本的宽度
    NSDictionary *attributes = @{NSFontAttributeName : kFontPingFang14};
    CGSize textSize = [self.priceLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    CGFloat width = textSize.width + 5;
    
    if (width <= 75) {
        // none 无需处理
        
    } else if (width > 100) {
        width = 100;//限制最大宽度
    }
    
    self.priceLabelWidthConstraint.constant = width;
}

- (void)setSelectCount:(NSInteger)selectCount{
    _selectCount = selectCount;
    [self.settlementBtn setTitle:[NSString stringWithFormat:@"去结算(%ld)", selectCount] forState:UIControlStateNormal];
}

- (void)setIsAllSelected:(BOOL)isAllSelected{
    _isAllSelected = isAllSelected;
    
    if (isAllSelected) {//已全选
        [self.allSelectedBtn setImage:[UIImage imageNamed:@"ic_pitchon"] forState:UIControlStateNormal];
    } else {//未全选
        [self.allSelectedBtn setImage:[UIImage imageNamed:@"ic_unchecked"] forState:UIControlStateNormal];
    }
}

- (void)setIsEditing:(BOOL)isEditing{
    if (isEditing) {//编辑状态
        self.totalLabel.hidden = YES;
        self.priceLabel.hidden = YES;
        [self.settlementBtn setTitle:@"删除" forState:UIControlStateNormal];
        [self.settlementBtn setBackgroundColor:[UIColor redColor]];
    } else {
        self.totalLabel.hidden = NO;
        self.priceLabel.hidden = NO;
        [self.settlementBtn setTitle:[NSString stringWithFormat:@"去结算(%ld)", (long)self.selectCount] forState:UIControlStateNormal];
        [self.settlementBtn setBackgroundColor:[UIColor orangeColor]];
    }
}

- (IBAction)buttonAction:(UIButton *)sender {

    if (sender.tag == 111) {//点击了全选按钮
        self.isAllSelected = !self.isAllSelected;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(shoppingCartBottomBar_buttonActionAtTag:)]) {
        [_delegate shoppingCartBottomBar_buttonActionAtTag:sender.tag];
    }
}

@end
